package com.nurese.controller;

import com.nurese.entity.Administrator;
import com.nurese.service.AdministratorService;
import com.nurese.utils.MapControll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.handler.MessageContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/administrator")
public class AdministratorController {

    @Autowired
    private AdministratorService administratorService;



    @PostMapping("/create")
    @ResponseBody
//    public void  create(Administrator administrator){
//        administratorService.create(administrator);
//    }
    public Map<String,Object> create(Administrator administrator){
//        administratorService.create(administrator);
        int result= administratorService.create(administrator);
        if (result<=0){
//            新增失败
            return MapControll.getInstance().error().getMap();

        }
        return MapControll.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = administratorService.delete(id);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @PostMapping("/update")
    @ResponseBody
//    @RequestBody
    public Map<String,Object> update(Administrator administrator){
        int result = administratorService.update(administrator);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @PostMapping("/detail/{id}")
    @ResponseBody
    public Map<String,Object> detail(@PathVariable("id") Integer id){

        Administrator administrator=administratorService.detail(id);
        if (administrator == null){
            return MapControll.getInstance().nodate().getMap();
        }
        return MapControll.getInstance().success().put("data",administrator).getMap();
    }

    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Administrator administrator){
        List<Administrator> list =administratorService.query(administrator);
        return MapControll.getInstance().success().put("data",list).getMap();
    }


}
